package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.OrdersCriteria;
import com.leo.ibatis.demo.domain.Orders;
import com.leo.ibatis.demo.service.OrdersService;

public class OrdersController extends MultiActionController {

	private OrdersService ordersService;

	public void setOrdersService(OrdersService ordersService) {
		this.ordersService = ordersService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrdersCriteria ordersCriteria = new OrdersCriteria();
		bind(request, ordersCriteria);
		
		List<Orders> ordersList = ordersService.listOnPage(ordersCriteria);
		
		ModelAndView mav = new ModelAndView("orders/list");
		mav.addObject("ordersList", ordersList);
		mav.addObject("ordersCriteria", ordersCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OrdersCriteria ordersCriteria = new OrdersCriteria();
		bind(request, ordersCriteria);
			
		int orderid = ServletRequestUtils.getRequiredIntParameter(request, "orderid");

		Orders orders = ordersService.select(orderid);
		
		ModelAndView mav = new ModelAndView("orders/detail");
		mav.addObject("orders", orders);
		mav.addObject("ordersCriteria", ordersCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int orderid = ServletRequestUtils.getRequiredIntParameter(request, "orderid");

		ordersService.delete(orderid);
		
		return new ModelAndView("redirect:orders.do");
	}

}
